package com.huawei.hms.framework.network.restclient.hwhttp.netdiag;

import android.os.SystemClock;
import com.huawei.hms.framework.common.Logger;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;

public class TcpConnectQuery implements IQuery {
    private static final String TAG = "TcpConnectQuery";
    private static final int TCP_PORT = 853;
    private static final int TIMEOUT = 5000;

    public QueryMetrics query(String str) {
        Socket socket;
        Throwable th;
        Socket socket2 = null;
        QueryMetrics queryMetrics = new QueryMetrics();
        long elapsedRealtime = SystemClock.elapsedRealtime();
        try {
            socket = new Socket();
            try {
                socket.connect(new InetSocketAddress(str, TCP_PORT), TIMEOUT);
                queryMetrics.setStatusCode(0);
                queryMetrics.setTotalTime(SystemClock.elapsedRealtime() - elapsedRealtime);
                Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + queryMetrics.getTotalTime());
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        Logger.w(TAG, "socket close occur IOException");
                    }
                }
            } catch (SocketTimeoutException e2) {
                try {
                    Logger.v(TAG, " TcpConnectQuery timeout");
                    queryMetrics.setStatusCode(2);
                    queryMetrics.setTotalTime(SystemClock.elapsedRealtime() - elapsedRealtime);
                    Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + queryMetrics.getTotalTime());
                    if (socket != null) {
                        try {
                            socket.close();
                        } catch (IOException e3) {
                            Logger.w(TAG, "socket close occur IOException");
                        }
                    }
                    return queryMetrics;
                } catch (Throwable th2) {
                    Throwable th3 = th2;
                    socket2 = socket;
                    th = th3;
                    queryMetrics.setTotalTime(SystemClock.elapsedRealtime() - elapsedRealtime);
                    Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + queryMetrics.getTotalTime());
                    if (socket2 != null) {
                        try {
                            socket2.close();
                        } catch (IOException e4) {
                            Logger.w(TAG, "socket close occur IOException");
                        }
                    }
                    throw th;
                }
            } catch (IOException e5) {
                Logger.v(TAG, " TcpConnectQuery fail ");
                queryMetrics.setStatusCode(1);
                queryMetrics.setTotalTime(SystemClock.elapsedRealtime() - elapsedRealtime);
                Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + queryMetrics.getTotalTime());
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e6) {
                        Logger.w(TAG, "socket close occur IOException");
                    }
                }
                return queryMetrics;
            }
        } catch (SocketTimeoutException e7) {
            socket = null;
            Logger.v(TAG, " TcpConnectQuery timeout");
            queryMetrics.setStatusCode(2);
            queryMetrics.setTotalTime(SystemClock.elapsedRealtime() - elapsedRealtime);
            Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + queryMetrics.getTotalTime());
            if (socket != null) {
                socket.close();
            }
            return queryMetrics;
        } catch (IOException e8) {
            socket = null;
            Logger.v(TAG, " TcpConnectQuery fail ");
            queryMetrics.setStatusCode(1);
            queryMetrics.setTotalTime(SystemClock.elapsedRealtime() - elapsedRealtime);
            Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + queryMetrics.getTotalTime());
            if (socket != null) {
                socket.close();
            }
            return queryMetrics;
        } catch (Throwable th4) {
            th = th4;
            queryMetrics.setTotalTime(SystemClock.elapsedRealtime() - elapsedRealtime);
            Logger.v(TAG, "  Status: " + queryMetrics.getStatusCode() + "   TotalTime: " + queryMetrics.getTotalTime());
            if (socket2 != null) {
                socket2.close();
            }
            throw th;
        }
        return queryMetrics;
    }
}
